package com.sinsz.c.properties;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;

/**
 * 传递mybatis参数
 * @author chenjianbo
 * @date 2017/11/21
 */
public class MybatisYmlProperties {

    private MybatisYmlProperties() {
    }

    private static final MybatisYmlProperties prop = new MybatisYmlProperties();

    public static MybatisYmlProperties instance() {
        return prop;
    }

    private final String config = "classpath:mybatis-config.xml";
    private final String mapperXml = "classpath:mapper/*.xml";

    public Resource getConfig(PathMatchingResourcePatternResolver resourceResolver) {
        return resourceResolver.getResource(config);
    }

    public Resource[] getMapperXml(PathMatchingResourcePatternResolver resourceResolver) {
        try {
            return resourceResolver.getResources(mapperXml);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Resource[]{};
    }

    @Override
    public String toString() {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace(System.out);
        }
        return super.toString();
    }
}